package com.cos.hospital.ui.record.edit;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.Selection;
import android.view.MotionEvent;
import android.view.View;

import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ErrorBean;
import com.cos.hospital.model.bean.RecordWebBean;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.tool.SPUtil;
import com.cos.hospital.tool.UKeyboard;
import com.cos.hospital.tool.UToast;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.ui.record.RecordBaseActivity;
import com.cos.hospital.ui.record.RecordCode;
import com.cos.hospital.widget.ProgressDialog;
import com.google.gson.JsonObject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * @Description: 编辑:主述、现病史、既往史、过敏史、辩证诊断
 * 页面纯文字编辑
 * @Author: Kosmos
 * @Date: 2018年09月23日 20:52
 * @Email: KosmoSakura@foxmail.com
 */
public class EditorChiefActivity extends RecordBaseActivity implements View.OnTouchListener {
    @BindView(R.id.rec_edt_main_lay)
    View rootLay;

    @Override
    protected int layout() {
        return R.layout.activity_editor_chief;
    }

    @Override
    protected void initData() {
        setTitleLeftIcon();
        setTitleRightTxt(getString(R.string.save));
        switch (fldEleID) {
            case RecordCode.Type.CurrentMedicalHistory://现病史:
                setTitleCenterTxt(getString(R.string.medical_edit));
                edtMain.setHint(R.string.please_enter_medicalf);
                break;
            case RecordCode.Type.PastHistory://既往史
                setTitleCenterTxt(getString(R.string.past_edit));
                edtMain.setHint(R.string.please_enter_past);
                break;
            case RecordCode.Type.AllergyHistory://过敏史
                setTitleCenterTxt(getString(R.string.allergy_edit));
                edtMain.setHint(R.string.please_enter_allergy);
                break;
            case RecordCode.Type.Diagnosis://辩证诊断
                setTitleCenterTxt(getString(R.string.diagnosis_edit));
                edtMain.setHint(R.string.please_enter_diagnosis);
                break;
            default://默认:主诉
                setTitleCenterTxt(getString(R.string.chief_edit));
                edtMain.setHint(R.string.please_enter_chief);
                break;
        }
        changeBtnState(tVoice, StateOFF);
        changeBtnState(tTemplate, StateOFF);
        changeBtnState(tFormula, StateNone);
        changeBtnState(tTakephoto, StateNone);
        initTemplateAdapter();
        rootLay.setOnTouchListener(this);
        edtMain.setOnTouchListener(this);
    }

    @Override
    protected void logic() {
        Selection.setSelection(edtMain.getText(), edtMain.length());
    }

    @Override
    protected void onTemplateSelect(String content) {
//        edtMain.append(content);
//        Selection.setSelection(edtMain.getText(), edtMain.length());
        int index = edtMain.getSelectionStart();
        Editable edit = edtMain.getEditableText();
        if (index < 0 || index >= edit.length()) {
            edtMain.append(content);
        } else {
            edit.insert(index, content);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        hideAllFun();
        UKeyboard.with(this).showForce(edtMain);
        return false;
    }

    @Override
    protected void onFunctionBtnClick(int state) {
        switch (state) {
            case FunTemplate:
                if (firstTimeToGetTemplateList) {
                    spv.callFreshDelay(500);
                    firstTimeToGetTemplateList = false;
                }
                break;
        }
    }

    @Override
    protected void onSpeakBack(String result) {
        int index = edtMain.getSelectionStart();
        Editable edit = edtMain.getEditableText();
        if (index < 0 || index >= edit.length()) {
            edtMain.append(result);
        } else {
            edit.insert(index, result);
        }
    }

    @OnClick({R.id.title_next_txt})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.title_next_txt://保存
                String content = edtMain.getText().toString();
                if (content.length() == 0) {
                    showNotice("还未输入文字内容");
                    return;
                }
                UKeyboard keyboard = UKeyboard.with(this);
                if (keyboard.isKeyboardShow()) {
                    keyboard.hideForce(edtMain);
                }
                save(content);
                break;
        }
    }

    private void save(String content) {
        ProgressDialog.getInstance().startProgressDialog(this);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("fldMedicalHistoryId", fldMedicalHistoryId);
        jsonObject.addProperty("fldEleID", fldEleID);
        jsonObject.addProperty("fldElementValue", content);
        jsonObject.addProperty("fldEditId", SPUtil.getUserId());
        UHttp.start(getServes().saveMedicalElement(jsonObject), "元素保存", new HttpMsgListener<ResultBean<RecordWebBean
            .MedicalElementBean>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                showNotice(describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultBean<RecordWebBean.MedicalElementBean> dto) {
                if (dto.isSuccess()) {
                    UToast.toast("保存成功");
                    Intent intent = getIntent();
                    intent.putExtra(Code.SP.CONTENT, content);
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                } else {
                    ErrorBean errorBean = dto.getError();
                    if (errorBean != null) {
                        showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                    } else {
                        showNotice(Code.DefaultPrompt);
                    }
                }
            }
        });
    }
}
